﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class EnvironmentalProfilesViewModel : INotifyPropertyChanged
    {
        private static EnvironmentalProfilesViewModel mInstance = null;
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<EnvironmentalProfileViewModel> Profiles { get; set; }
        private EnvironmentalProfileViewModel mProfile = null;
        private string mXmlElementName = "EnvironmentalProfiles";

        public EnvironmentalProfileViewModel Profile
        {
            get { return mProfile; }
            set
            {
                mProfile = value;
                RaisePropertyChanged("Profile");
            }
        }

        private EnvironmentalProfilesViewModel()
        {
            if (null == Profiles) Profiles = new ObservableCollection<EnvironmentalProfileViewModel>();
        }

        public static EnvironmentalProfilesViewModel GetInstance()
        {
            if (null == mInstance) mInstance = new EnvironmentalProfilesViewModel();
            return mInstance;
        }

        public XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (null == doc) return _element;
            if (0 < Profiles.Count)
            {
                _element = doc.CreateElement(mXmlElementName);
                foreach (EnvironmentalProfileViewModel _profile in Profiles) _element.AppendChild(_profile.Export(doc));
            }
            return _element;
        }

        public void Import(XmlElement element)
        {
            if (null == element) return;
            if (mXmlElementName == element.Name)
            {
                Profiles.Clear();
                if (element.HasChildNodes) foreach (XmlNode _child in element.ChildNodes)
                    {
                        EnvironmentalProfileViewModel _profile = new EnvironmentalProfileViewModel();
                        _profile.Import(_child as XmlElement);
                        Profiles.Add(_profile);
                    }
            }
        }

        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}
